package cn.tedu.oa.sys.controller;

import cn.tedu.oa.common.annotation.LogAnnotation;
import cn.tedu.oa.common.util.ShiroUtils;
import cn.tedu.oa.common.vo.JsonResult;
import cn.tedu.oa.sys.entity.Role;
import cn.tedu.oa.sys.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sys/role/")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /*角色跳转首页*/
    @LogAnnotation("角色:角色跳转首页")
    @RequestMapping("role_ui")
    public String doRoleUI() {
        return "sys/role/role_ui";
    }

    /*角色添加跳转页面*/
    @LogAnnotation("角色:角色添加跳转页面")
    @RequestMapping("role_edit")
    public String doUserEditUI() {
        return "sys/role/role_edit";
    }

    /*角色修改跳转页面*/
    @LogAnnotation("角色:角色修改跳转页面")
    @RequestMapping("role_update")
    public String doUserUpdateUI() {
        return "sys/role/role_update";
    }

    /*角色授权跳转页面*/
    @LogAnnotation("角色:角色授权跳转页面")
    @RequestMapping("role_accredit")
    public String doUserAccreditUI() {
        return "sys/role/role_accredit";
    }

    @LogAnnotation("角色:角色删除")
    @RequestMapping("deleteId")
    @ResponseBody
    public JsonResult delete(String id) {
        int id1 = Integer.parseInt(id);
        roleService.delete(id1);
        return new JsonResult("delete ok");
    }

    /*显示数据列表*/
    @LogAnnotation("角色:分页查询所有")
    @RequestMapping("get")
    @ResponseBody
    public JsonResult get(String criteria, Integer pageNo, Integer pageSize) {
        List<Role> list = roleService.getUserPagerList(criteria, pageNo, pageSize);
        int rowCount = roleService.getRowCount(criteria);
        JsonResult result = new JsonResult();
        result.setCount(rowCount);
        result.setData(list);
        return result;
    }

    /*添加数据*/
    @LogAnnotation("角色:角色添加")
    @RequestMapping("getAdd")
    @ResponseBody
    public JsonResult getAdd(Role role) {
        role.setAddUser(ShiroUtils.getPrincipal().getName());
        role.setAddTime(new Date());
        roleService.add(role);
        return new JsonResult("添加成功");
    }

    @LogAnnotation("角色:角色修改")
    @RequestMapping("getUpdate")
    @ResponseBody
    public JsonResult getUpdate(Role role) {
        role.setUpdateUser(ShiroUtils.getPrincipal().getName());
        role.setUpdateTime(new Date());
        roleService.update(role);
        return new JsonResult("修改成功!");
    }

    /*根据id删除用户*/
    @LogAnnotation("角色:根据id删除用户")
    @RequestMapping("deleteByIds")
    @ResponseBody
    public JsonResult deleteByIds(Integer... ids) {
        roleService.deleteByIds(ids);
        return new JsonResult("删除成功");
    }

    /*添加用户*/
    @LogAnnotation("角色:添加角色")
    @RequestMapping("addRole")
    @ResponseBody
    public JsonResult addUser(Role role) {
        roleService.add(role);
        return new JsonResult("添加成功");
    }

    @LogAnnotation("角色:根据角色id授权")
    @RequestMapping("role_menu_add")
    @ResponseBody
    public JsonResult doSaveObject(Integer roleId, Integer[] menuId,Integer isdel) {

        roleService.insertMenuByRole(roleId, menuId,isdel);
        return new JsonResult("授权成功！");

    }

}
